package ace.cmp.application.core.controller;

import ace.cmp.application.core.support.handler.WebExceptionHandler;
import ace.cmp.core.model.R;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理.
 *
 * @author Caspar
 */
@Slf4j
@RestControllerAdvice
@AllArgsConstructor
public class GlobalErrorRestControllerAdvice {

  private final WebExceptionHandler webExceptionHandler;

  @ExceptionHandler(value = Exception.class)
  @ResponseBody
  public R<Object> error(HttpServletRequest request, HttpServletResponse response, Exception ex) {
    return webExceptionHandler.execute(request, response, null, ex);
  }

  @RequestMapping(path = "/throw/runtime/exception")
  public R<Object> throwRuntimeException() {
    log.info("/throw/runtime/exception access");
    throw new RuntimeException("1111");
  }
}
